package com.blog.controller.backend;


import com.baomidou.mybatisplus.plugins.Page;
import com.blog.entity.Order;
import com.blog.entity.Product;
import com.blog.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mars123
 * @since 2019-07-02
 */
@Controller("backendOrder")
@RequestMapping("/backend/order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    @RequestMapping("/index")
    public String index(Model model, Integer curPage) {
        model.addAttribute("page", orderService.index(null, new Page<Order>(curPage == null ? 1 : curPage, 5)));
        return "backend/order/index";
    }

    @RequestMapping("/delete")
    public String create(Integer id) {
        orderService.delete(id);
        return "redirect:/backend/order/index";
    }

    @RequestMapping("/update")
    public String update(@ModelAttribute("order") Order order, Model model, Integer id, HttpServletRequest request) {
        if (request.getMethod().equals("POST") && orderService.update(order) != null) {
            return "redirect:/backend/order/view?id=" + order.getId();
        }
        model.addAttribute("order", orderService.view(id));
        return "backend/order/update";
    }

    @RequestMapping("/view")
    public String view(Integer id, Model model) {
        model.addAttribute("order", orderService.view(id));
        return "backend/order/view";
    }
}

